/**
 * Toast单例版
 */

import _extends from '@babel/runtime/helpers/esm/extends'
import { Toast } from 'vant'

let toast

function SingletonToast (options = {}) {
  clearSingletonToast()
  toast = new Toast(options)
  return toast
}

function clearSingletonToast () {
  if (toast) {
    // 手动清除Toast
    toast.clear()
  }
}

function createMethod (type) {
  return function (options) {
    return SingletonToast(_extends({
      type
    }, parseOptions(options)))
  }
}
function parseOptions (message) {
  if (isObj(message)) {
    return message
  }
  return {
    message
  }
}
function isObj (x) {
  var type = typeof x
  return x !== null && (type === 'object' || type === 'function')
}

['loading', 'success', 'fail'].forEach(method => {
  SingletonToast[method] = createMethod(method)
})

SingletonToast.clear = function (all) {
  if (all) {
    Toast.clear(all)
  } else {
    clearSingletonToast()
  }
}

export default SingletonToast
